<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="addBtn">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addOrUpdateHandle('add')">新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="editBtn">
        <el-button type="success" :disabled="single" plain icon="el-icon-edit" size="mini" @click="addOrUpdateHandle()">修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="delectBtn">
        <el-button type="danger" :disabled="multiple" plain icon="el-icon-delete" size="mini" @click="deleteHandle">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="dataListSelectionChangeHandle">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="qrTitle" />
      <el-table-column label="二维码内容" align="center" prop="qrContent" />
      <el-table-column label="开关" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch :active-value="1" :inactive-value="0" v-model="scope.row.status" @change="changeSwitch(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否需要推广人员" align="center" prop="needBd">
        <template slot-scope="scope">
          <el-switch :active-value="1" :inactive-value="0" v-model="scope.row.needBd" @change="changeSwitch(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="是否有时效性" align="center" prop="timeEffectiveSwitch">
        <template slot-scope="scope">
          <el-switch :active-value="1" :inactive-value="0" v-model="scope.row.timeEffectiveSwitch" @change="changeSwitch(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="时效时长（分钟）" align="center" prop="timeEffectiveDuration" />
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width" v-if="type == 'indexList'">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-connection" @click="bindQrocde(scope.row)">绑定二维码组</el-button>
          <el-button size="mini" type="text" icon="el-icon-connection" @click="noBindQrocde(scope.row)">解绑二维码组</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :current-page.sync="pageNum" :page.sync="pageNum" :limit.sync="pageSize" @pagination="query" />

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <ChooseDialog v-if="ChooseVisible" :getType="getType" ref="groupChoose" @getValue="getValue" />
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './qrcodeContent/index-add-or-update.vue'
import { qrcodeContentSave, qrcodeContentBindGroup,unbindGroupBindGroup } from "@/api/basic/qrcodeContent"
import ChooseDialog from './qrcodeGroup/chooseDialog.vue'
export default {
  name: "QrcodeContent",
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/qrcodeContent/list',
        getDataListIsPage: true,
        deleteURL: '/qrcodeContent/remove',
        deleteIsBatch: true
      },
      queryParams: {
        city: '',
        street: '',
        address: ''
      },
      // 表单参数
      form: {},
      areaList: [],
      confirmLoading: false,
      // 表单校验
      rules: {
      },
      ChooseVisible: false,
      getType: ''
    };
  },
  props: {
    //  type                              使用列表的状态 indexList为后面有操作权限
    //  editBtn                           是否显示修改按钮
    //  addBtn                            是否显示新增按钮
    //  delectBtn                         是否显示删除按钮
    type: {
      type: String,
      default: ''
    },
    editBtn: {
      type: Boolean,
      default: false
    },
    addBtn: {
      type: Boolean,
      default: false
    },
    delectBtn: {
      type: Boolean,
      default: false
    },
  },
  components: {
    AddOrUpdate, //新增修改的状态弹窗
    ChooseDialog
  },
  created() {

  },
  methods: {
    // 点击开关 值改变
    changeSwitch(row) {
      let params = {
        id: row.id,
        status: row.status,
        needBd: row.needBd,
        timeEffectiveSwitch: row.timeEffectiveSwitch,
      }
      qrcodeContentSave(params).then(response => {
        this.$modal.msgSuccess("操作成功");
      });
    },
    // 绑定二维码组
    bindQrocde(row) {
      this.getType = 'bind'
      this.ChooseVisible = true
      this.$nextTick(() => {
        this.$refs.groupChoose.init()
        this.$refs.groupChoose.rowId = row.id
      })
    },
    // 解绑二维码组
    noBindQrocde(row) {
      this.getType = 'noBind'
      this.ChooseVisible = true
      this.$nextTick(() => {
        this.$refs.groupChoose.init()
        this.$refs.groupChoose.rowId = row.id
      })
    },
    // 绑定二维码组
    getValue(value, rowId) {
      let data = {}
      data.contentId = rowId
      data.groupId = value.id
      if (this.getType == 'bind') {
        qrcodeContentBindGroup(data).then((res) => {
          this.$modal.msgSuccess("绑定成功");
        })
      }else{
        unbindGroupBindGroup(data).then((res) => {
          this.$modal.msgSuccess("解绑成功");
        })
      }
    },
  }
};
</script>
